package com.example.studyllm.service.rag;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.studyllm.entity.rag.RagKnowledge;
import com.example.studyllm.mapper.rag.RagKnowledgeMapper;
import com.example.studyllm.service.base.BaseServiceImpl;
import com.example.studyllm.utils.BeanUtils;
import com.example.studyllm.vo.base.PageResultVO;
import org.springframework.stereotype.Service;
import cn.hutool.core.collection.CollectionUtil;

/**
 * <p>
 *   service实现类
 * </p>
 *
 * @author kw
 * @since 2025-02-05 15:12:48.825120
 */
@Service
public class RagKnowledgeService extends BaseServiceImpl<RagKnowledgeMapper, RagKnowledge> {


    public Boolean addRagKnowledge(RagKnowledge ragKnowledge) {
        return this.save(ragKnowledge);
    }

    public Boolean updateRagKnowledgeById(RagKnowledge ragKnowledge) {
        return this.updateById(ragKnowledge);
    }

    public PageResultVO<RagKnowledge> getPage(RagKnowledge pageParam) {

        LambdaQueryWrapper<RagKnowledge> lambda = new QueryWrapper<RagKnowledge>().lambda();
        this.buildCondition(lambda, pageParam);
        lambda.orderBy(true, false, RagKnowledge::getId);           
        PageResultVO<RagKnowledge> resultPage = BeanUtils.changePage(RagKnowledge.class, this.page(new Page<>(pageParam.getPageNo(), pageParam.getPageSize()), lambda));
        return resultPage;
    }
    /**
     * 构造查询条件
     *
     * @param lambda
     * @param param
     */
    private void buildCondition(LambdaQueryWrapper<RagKnowledge> lambda, RagKnowledge param) {
        
            lambda.eq(ObjectUtil.isNotEmpty(param.getId()),RagKnowledge::getId, param.getId());
        
            lambda.eq(ObjectUtil.isNotEmpty(param.getCode()),RagKnowledge::getCode, param.getCode());
        
            lambda.eq(ObjectUtil.isNotEmpty(param.getEmbeddingStoreId()),RagKnowledge::getEmbeddingStoreId, param.getEmbeddingStoreId());
        
            lambda.eq(ObjectUtil.isNotEmpty(param.getEmbeddingModelId()),RagKnowledge::getEmbeddingModelId, param.getEmbeddingModelId());
        
            lambda.eq(ObjectUtil.isNotEmpty(param.getName()),RagKnowledge::getName, param.getName());
        
            lambda.eq(ObjectUtil.isNotEmpty(param.getDescr()),RagKnowledge::getDescr, param.getDescr());
        
            lambda.eq(ObjectUtil.isNotEmpty(param.getCtime()),RagKnowledge::getCtime, param.getCtime());
        
            lambda.eq(ObjectUtil.isNotEmpty(param.getUtime()),RagKnowledge::getUtime, param.getUtime());
        
            lambda.eq(ObjectUtil.isNotEmpty(param.getDeleteFlag()),RagKnowledge::getDeleteFlag, param.getDeleteFlag());
        
            lambda.eq(ObjectUtil.isNotEmpty(param.getCby()),RagKnowledge::getCby, param.getCby());
        
            lambda.eq(ObjectUtil.isNotEmpty(param.getUby()),RagKnowledge::getUby, param.getUby());
        
    }
}